import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;

/**
 * @author long
 * @date 2013-4-27 下午4:25:52
 * @qq 108232706
 * @description ServerJniMgr
 */
public class test {

	static {
		System.loadLibrary("test");
	}

	public static void main0(String[] args) {
		System.out.println("Hello ServerJniMgr!");

		// receive(new byte[72]);

		System.out.println("ready call native send(bytes[])");
		send(new byte[20]);
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("=======ServerJniMgr over==============");
	}

	public static void main(String[] args) {
		// testByteChar();
//		SystemUtil.printSystemProperty();
	}

	private static void testByteChar() {
		String words = "i love 龙";
		try {

			// 支持中文,3个字节一个中文,length:10 content:105 32 108 111 118 101 32 -23 -66
			// -103
			// bytesWord:i love 龙
			String encode = "utf-8";
			// String newStr = new String(str.getBytes("GB2312"),"ISO-8859-1");
			byte[] bytes = words.getBytes(encode);
			printBytes(bytes);
			String bytesWord = new String(bytes, encode);
			System.out.println("bytesWord:" + bytesWord);

			// 支持中文,2字节一位 ,8：105 32 108 111 118 101 32 -63 -6
			// bytesWord:i love 龙
			encode = "GB2312";
			bytes = words.getBytes(encode);
			printBytes(bytes);
			bytesWord = new String(bytes, encode);
			System.out.println("bytesWord:" + bytesWord);

			// 不支持中文 8：105 32 108 111 118 101 32 63
			// bytesWord:i love ?
			encode = "ISO-8859-1";
			bytes = words.getBytes(encode);
			printBytes(bytes);

			bytesWord = new String(bytes, encode);
			System.out.println("bytesWord:" + bytesWord);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

	}

	private static void printBytes(byte[] bytes) {

		StringBuilder temp = new StringBuilder("length:" + bytes.length
				+ " content:\r\n");
		for (int i = 0; i < bytes.length; i++) {
			if (i % 16 == 0)
				temp.append("\r\n");
			temp.append(bytes[i] + " ");
		}
		System.out.println(temp.toString());
	}

	/**
	 * javac++
	 * 
	 * @param bytes
	 */
	public static native void send(byte[] bytes);

	/**
	 * c++java
	 * 
	 * @param bytes
	 */
	public static void receive(byte[] bytes) {

		StringBuilder strTmp = new StringBuilder(":" + bytes.length + ";:");
		testtest data = new testtest();

		int start = readIntArray(bytes, 0, data.i0, data.i0.length);
		strTmp.append("get i0:\r\n");
		for (int i = 0; i < data.i0.length; i++) {
			strTmp.append(i + ":" + bytes[i] + " ");
		}

		data.b1 = readByte(bytes, start);
		start++;

		data.i1 = readInt(bytes, start);
		start += 4;

		data.f0 = readFloat(bytes, start);
		start += 4;

		data.f1 = readDouble(bytes, start);
		start += 8;

		data.b0 = readBool(bytes, start);
		start++;

		data.f3 = readFloat(bytes, start);
		start += 4;

		try {
			int len = bytes.length - start;
			data.str = new String(bytes, start, len, "utf-8").toCharArray();
			start += len;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		// start = readCharArray(bytes, start, data.str, data.str.length);

		strTmp.append(data.info());
		// a b c d

		System.out.println(strTmp.toString());
	}

	public static int readIntArray(byte[] bytes, int start, int[] destInt,
			int len) {

		if (destInt.length < len)
			return start;
		for (int j = 0; j < len; j++) {
			destInt[j] = readInt(bytes, start);
			start += 4;
		}
		return start;
	}

	private static String hexString = "0123456789ABCDEF";

	public static int readCharArray(byte[] bytes, int start, char[] destchar,
			int len) {
		if (destchar.length < len)
			return start;
		// Charset cs = Charset.forName ("UTF-8");
		// cs.decode(bb)
		int temp;
		for (int j = 0; j < len; j++) {
			temp = bytes[j] & 0xf0 >> 4 + bytes[j] & 0x0f;
			// sb.append(hexString.charAt((bytes[j]&0xf0)>>4));
			// sb.append(hexString.charAt((bytes[j]&0x0f)>>0));
			destchar[j] = (char) temp;
			start++;
		}
		return start;
	}

	public static boolean readBool(byte[] bytes, int i) {
		return (bytes[i] > 0);
	}

	public static byte readByte(byte[] bytes, int i) {
		return bytes[i];
	}

	public static float readFloat(byte[] bytes, int i) {
		return Float.intBitsToFloat(readInt(bytes, i));
	}

	public static double readDouble(byte[] bytes, int i) {
		return Double.longBitsToDouble(readLong(bytes, i));
	}

	public static long readLong(byte[] bytes, int i) {
		return ((((long) bytes[i + 7] & 0xff) << 56)
				| (((long) bytes[i + 6] & 0xff) << 48)
				| (((long) bytes[i + 5] & 0xff) << 40)
				| (((long) bytes[i + 4] & 0xff) << 32)
				| (((long) bytes[i + 3] & 0xff) << 24)
				| (((long) bytes[i + 2] & 0xff) << 16)
				| (((long) bytes[i + 1] & 0xff) << 8) | (((long) bytes[i] & 0xff) << 0));
	}

	public static int saveSerializable(Object obj, String fileName) {

		return 0;
	}

	public static Object readSerializable(String fileName) {

		return null;
	}

	public static int readInt(byte[] bytes, int i) {
		return ((((bytes[i + 3] & 0xff) << 24)
				| ((bytes[i + 2] & 0xff) << 16) | ((bytes[i + 1] & 0xff) << 8) | ((bytes[i] & 0xff) << 0)));
	}

	public static class testtest implements Serializable {
		private static final long serialVersionUID = 5228992643265642987L;
		// 82byte
		int[] i0;
		byte b1;
		int i1;
		float f0;
		double f1;
		boolean b0;
		float f3;
		char[] str;

		public testtest() {
			i0 = new int[10];
			b1 = 0;
			str = new char[20];
		}

		public String info() {
			StringBuilder sb = new StringBuilder("i0:");
			for (int i = 0; i < i0.length; i++) {
				sb.append(i0[i] + " ");
			}
			sb.append("\r\nb1:" + b1);
			sb.append("\r\ni1:" + i1);
			sb.append("\r\nf0:" + f0);
			sb.append("\r\nb0:" + b0);
			sb.append("\r\nf3:" + f3);
			sb.append("\r\nstr:\r\n");
			for (int i = 0; i < str.length; i++) {
				sb.append("{" + str[i] + "}");
			}
			return sb.toString();
		}
	}

	static long first;
	static long second;

	public static void start() {
		first = System.currentTimeMillis();
		System.out.println("start ms:" + first);
	}

	public static void stop() {
		second = System.currentTimeMillis();
		System.out.println("stop ms:" + second);
		System.out.println("cost ms:" + (second - first));
	}

}
